import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: hp
 * Date: 2024-05-13
 * Time: 22:14
 */
public class Test {
    public static boolean containsDuplicate(int[] nums) {
        HashMap<Integer,Integer> map = new HashMap<>();
        for(int i = 0; i < nums.length; i++) {
            if(!map.isEmpty() && map.containsKey(nums[i])) {
                return true;
            }else {
                map.put(nums[i],1);
            }
        }
        return false;
    }

    public static boolean containsNearbyDuplicate(int[] nums, int k) {
        HashMap<Integer,Integer> map = new HashMap<>();
        for(int i = 0; i < nums.length; i++) {
            if(!map.isEmpty() && map.containsKey(nums[i])) {
                int jj = i;
                for(int h = 0; h < i; h++) {
                    if(nums[h] == nums[i]) {
                        int ii = h;
                        if(jj - ii <= k) {
                            return true;
                        }
                    }
                }
            }else {
                map.put(nums[i],1);
            }
        }
        return false;
    }

}
